package gui;

import java.util.ArrayList;

import javax.swing.JPanel;

import Entities.AssessmentEntity;
import Entities.RequestEntity;
import common.ChatIF;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.Box;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * 
 * @author Idan Mann
 * This reusable GUI class loads a request and presents it's details.
 * This services all users of the system other than the initiator.
 * This class is versatile and will change it's purpose depending on the user's access level.
 * It presents read only data to the reviewers and write permissions to the supervisor.
 *
 */
public class ViewAssessmentPanel extends JPanel implements ChatIF
{
	private JTextField viewingTextField;
	private JTextField firstNameField;
	private JTextField lastNameField;
	private JTextField emailField;
	private JTextField departmentField;
	private JTextField titleField;
	private JTextField phoneField;
	private JTextField locationField;
	private JTextArea requieredChangeDescriptionTextArea;
	private JTextArea expectedResultsAndBenefitsTextArea;
	private JTextArea constrictionsTextArea;
	private String assessmentID;
	private String aid;
	private JButton btnClose;
	private AcademicFrame frame;
	private JPanel previousPanel;
	private JTextField assessedDurationTextField;

		
	
	public ViewAssessmentPanel(String assessmentID, AcademicFrame frame, JPanel previousPanel)
	{
		this.assessmentID = assessmentID;
		this.frame = frame;
		this.previousPanel=previousPanel;

		initialize();
		
		
		
	}
		private void initialize()
		{
			setLayout(null);
				
			JLabel lblViewing = new JLabel("Viewing Assessment");
			lblViewing.setBounds(116, 33, 129, 14);
			add(lblViewing);
			
			viewingTextField = new JTextField();
			viewingTextField.setEditable(false);
			viewingTextField.setBounds(253, 30, 99, 20);
			add(viewingTextField);
			viewingTextField.setColumns(10);
			
			JLabel lblFirstName = new JLabel("First name");
			lblFirstName.setBounds(32, 98, 73, 14);
			add(lblFirstName);
			
			firstNameField = new JTextField();
			firstNameField.setEditable(false);
			firstNameField.setBounds(104, 95, 141, 20);
			add(firstNameField);
			firstNameField.setColumns(10);
			
			JLabel lblInitiatorDetails = new JLabel("Assessor Details:");
			lblInitiatorDetails.setBounds(32, 68, 120, 14);
			add(lblInitiatorDetails);
			
			JLabel lblLastName = new JLabel("Last name");
			lblLastName.setBounds(32, 131, 73, 14);
			add(lblLastName);
			
			lastNameField = new JTextField();
			lastNameField.setEditable(false);
			lastNameField.setColumns(10);
			lastNameField.setBounds(104, 128, 141, 20);
			add(lastNameField);
			
			emailField = new JTextField();
			emailField.setEditable(false);
			emailField.setColumns(10);
			emailField.setBounds(351, 95, 141, 20);
			add(emailField);
			
			JLabel lblEmail = new JLabel("E-mail");
			lblEmail.setBounds(279, 98, 73, 14);
			add(lblEmail);
			
			JLabel lblDepartment = new JLabel("Department");
			lblDepartment.setBounds(279, 131, 73, 14);
			add(lblDepartment);
			
			departmentField = new JTextField();
			departmentField.setEditable(false);
			departmentField.setColumns(10);
			departmentField.setBounds(351, 128, 141, 20);
			add(departmentField);
			
			titleField = new JTextField();
			titleField.setEditable(false);
			titleField.setColumns(10);
			titleField.setBounds(104, 162, 141, 20);
			add(titleField);
			
			JLabel lblTitle = new JLabel("Title");
			lblTitle.setBounds(32, 165, 73, 14);
			add(lblTitle);
			
			phoneField = new JTextField();
			phoneField.setEditable(false);
			phoneField.setColumns(10);
			phoneField.setBounds(351, 162, 141, 20);
			add(phoneField);
			
			JLabel lblPhone = new JLabel("Phone");
			lblPhone.setBounds(279, 165, 73, 14);
			add(lblPhone);
			
			JLabel lblRequestDetails = new JLabel("Assessment Details:");
			lblRequestDetails.setBounds(32, 200, 141, 14);
			add(lblRequestDetails);
			
			JLabel lblLocation = new JLabel("Location");
			lblLocation.setBounds(32, 231, 73, 14);
			add(lblLocation);
			
			locationField = new JTextField();
			locationField.setEditable(false);
			locationField.setBounds(104, 228, 142, 20);
			add(locationField);
			locationField.setColumns(10);
			
			JLabel lblRequieredChangeDescription = new JLabel("Requiered change description");
			lblRequieredChangeDescription.setBounds(32, 256, 205, 14);
			add(lblRequieredChangeDescription);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(32, 281, 461, 100);
			add(scrollPane);
			
			requieredChangeDescriptionTextArea = new JTextArea();
			scrollPane.setViewportView(requieredChangeDescriptionTextArea);
			requieredChangeDescriptionTextArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
			requieredChangeDescriptionTextArea.setEditable(false);
			
			JLabel lblExpectedResultsAndBenefits = new JLabel("Expected results and benefits");
			lblExpectedResultsAndBenefits.setBounds(32, 392, 205, 14);
			add(lblExpectedResultsAndBenefits);
			
			JScrollPane scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(32, 417, 457, 105);
			add(scrollPane_1);
			
			expectedResultsAndBenefitsTextArea = new JTextArea();
			expectedResultsAndBenefitsTextArea.setEditable(false);
			scrollPane_1.setViewportView(expectedResultsAndBenefitsTextArea);
			expectedResultsAndBenefitsTextArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
			
			JLabel lblConstrictions = new JLabel("Constrictions");
			lblConstrictions.setBounds(32, 533, 99, 14);
			add(lblConstrictions);
			
			JScrollPane scrollPane_2 = new JScrollPane();
			scrollPane_2.setBounds(32, 552, 457, 101);
			add(scrollPane_2);
			
			constrictionsTextArea = new JTextArea();
			constrictionsTextArea.setEditable(false);
			scrollPane_2.setViewportView(constrictionsTextArea);
			constrictionsTextArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
			
			btnClose = new JButton("Close");
			btnClose.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					frame.setSize(640, 540);
					frame.setContentPane(previousPanel);	
				}
			});
			btnClose.setBounds(210, 664, 91, 23);
			add(btnClose);
			
			JLabel lblAssessedDuration = new JLabel("Duration");
			lblAssessedDuration.setBounds(278, 231, 60, 14);
			add(lblAssessedDuration);
			
			assessedDurationTextField = new JTextField();
			assessedDurationTextField.setEditable(false);
			assessedDurationTextField.setColumns(10);
			assessedDurationTextField.setBounds(351, 228, 141, 20);
			add(assessedDurationTextField);
			
			JButton btnFetch = new JButton("Fetch");
			btnFetch.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					ArrayList<Object> getAssessment = new ArrayList<Object>();
					getAssessment.add("viewAssessment");
					getAssessment.add(assessmentID);
					AcademicFrame.myclient.sendMessageToServer(getAssessment);
				}
			});
			btnFetch.setBounds(373, 29, 89, 23);
			add(btnFetch);
		}

	@Override
	public void display(Object message)
	{
	
		
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;

		
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("assessmentData")	)
		{
			if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
			{
				AssessmentEntity assessmentDetails = (AssessmentEntity)(answer.get(2));
				
				// Start filling the form
								
				this.viewingTextField.setText(assessmentID);
				this.assessedDurationTextField.setText(assessmentDetails.getDurationAssessment() + " days");
				this.requieredChangeDescriptionTextArea.setText(assessmentDetails.getChangeDescription());
				this.expectedResultsAndBenefitsTextArea.setText(assessmentDetails.getBenefit());
				this.constrictionsTextArea.setText(assessmentDetails.getConstrictions());
				this.aid = assessmentDetails.getAssessorID();
				this.locationField.setText(assessmentDetails.getLocation());
				
				ArrayList<Object> getInitiatorInfo = new ArrayList<Object>();
				getInitiatorInfo.add("getInfo");
				getInitiatorInfo.add(aid);
				
				AcademicFrame.myclient.sendMessageToServer(getInitiatorInfo);
				
			}
			else
			{
				(new Dialog ( (String)(answer.get(1)) )).setVisible(true);
				frame.setContentPane(previousPanel);
				frame.setSize(500, 400);
			}

		}
		
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("userInfo")	)
				{
					this.firstNameField.setText((String)answer.get(1));
					this.lastNameField.setText((String)answer.get(2));
					this.emailField.setText((String)answer.get(3));
					this.titleField.setText((String)answer.get(4));
					this.departmentField.setText((String)answer.get(5));
					this.phoneField.setText((String)answer.get(6));
					
				}
				
				
		
	}
	
	public String getassessmentID()
	{
		return assessmentID;
	}
}
